<template>
  <div>
    <!-- 主题图片 -->
    <img-box
      width="100%"
      :height="200"
      :imgSrc="themePic"
    ></img-box>

    <!-- 内容区域 -->
    <div class="wrap xui-margin-bottom-xxl">
      <h3>长沙博物馆用户注册</h3>
      <el-form ref="form" :model="form" :rules="form.rule" label-width="85px" class="xui-padding-top-sm">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username" placeholder="请设置用户名"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="mobile">
          <el-input v-model="form.mobile" placeholder="可用于找回密码"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" type="password" placeholder="请设置登录密码"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="rePassword">
          <el-input v-model="form.rePassword" type="password" placeholder="请设置登录密码"></el-input>
        </el-form-item>
        <el-form-item class="btn">
          <el-button type="primary" @click="onSubmit">注册</el-button>
          <el-button type="primary" plain @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
      <span>已有账号，请直接<i class="color-red cursor-point" @click="login">登录</i></span>
    </div>

  </div>
</template>

<script>
import imgBox from "@/components/widge/img-box";
import themePic from "@/assets/images/example/theme.png";
import { register, loginByUsername } from "@/api/login";

export default {
  data() {
    var validateRePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.form.password) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      themePic,
      form: {
        username: "",
        password: "",
        rePassword: "",
        mobile: "",
        rule: {
          username: [
            { required: true, message: "请输入用户名", trigger: "blur" },
            {
              min: 5,
              max: 15,
              message: "用户名长度为5～15之间",
              trigger: "blur"
            }
          ],
          password: [
            { required: true, message: "请设置密码", trigger: "blur" },
            {
              min: 6,
              max: 15,
              message: "用户名长度为6～15之间",
              trigger: "blur"
            },
            {
              pattern: /^(?=.*[a-z])(?=.*[0-9])[a-zA-Z0-9]{6,15}$/,
              message: "密码为数字和英文的组合，不包含特殊字符，6-15位。",
              trigger: "blur"
            }
          ],
          rePassword: [
            { required: true, message: "请再次输入密码", trigger: "blur" },
            { validator: validateRePass, trigger: "blur" }
          ],
          mobile: [
            {
              required: true,
              message: "请输入手机号码",
              trigger: "blur"
            },
            {
              pattern: /^1[3|4|5|6|7|8][0-9]\d{4,8}$/,
              message: "请输入正确的手机号码",
              trigger: "blur"
            }
          ]
        }
      }
    };
  },
  components: {
    "img-box": imgBox
  },
  methods: {
    onSubmit() {
      this.$refs["form"].validate(valid => {
        if (!valid) {
          this.$message({
            showClose: true,
            type: "error",
            message: "提交失败，请检查填写的信息是否符合要求。"
          });
          return false;
        }
        register(this.form.username, this.form.password, this.form.mobile).then(
          res => {
            if (res.flag == 1) {
              this.$message({
                showClose: true,
                type: "success",
                message: res.msg
              });
              this.$alert("为了更好的体验请补充个人信息", res.msg, {
                confirmButtonText: "确定",
                type: "success",
                center: true,
                callback: action => {
                  this.loginAndGotoEdit();
                }
              });
            } else {
              this.$message({
                showClose: true,
                type: "error",
                message: res.msg
              });
            }
          }
        );
      });
    },
    loginAndGotoEdit() {
      console.log(this.username, this.password);
      this.$store.dispatch("LoginByUsername", this.form).then(res => {
        this.$router.push({ name: "userCenter-myInfo-edit" });
      });
    },
    reset() {
      this.$refs.form.resetFields();
    },
    login() {
      this.$store.dispatch("ToggleLoginDialog", true);
    }
  }
};
</script>

<style lang="less" scoped>
.wrap {
  width: 1095px;
  margin: 0 auto 40px;
  h3 {
    color: #626262;
    border-bottom: 1px solid #e8e8e8;
    padding: 20px 0;
    text-align: center;
  }
  .el-form {
    width: 418px;
    margin: 0 auto;
  }
  span {
    display: block;
    text-align: center;
    margin-left: 54px;
  }
}
.btn {
  text-align: center;
}
.el-button {
  display: inline-block;
  padding: 12px 40px;
}
</style>


